/**
 * @Author：ming
 * @Date：2023/10/2 1:11 AM
 * @Filename：Lc19
 */

/**
 * @author ming
 */
public class Lc19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int len =0;
        ListNode q = head;
        //求链表长度
        while (q!=null){
            len++;
            q =q.next;
        }
        //倒数n，则顺数k
        int k = len -n;
        if(k==0){
            return head.next;
        }

        int count =0;
        ListNode  qPre = head;
        //找倒第k个结点和前一个结点
        while (count<k-1){
            qPre = qPre.next;
        }
        q= qPre.next;
        qPre.next = q.next;

        return head;

    }
}
